local xiwei = fk.CreateSkill{
  name = "plum__xiwei",
}

Fk:loadTranslationTable{
  [xiwei.name] = "隙围",
  [":"..xiwei.name] = "出牌阶段限一次，你可以令一名其他角色将一至两张牌置于牌堆顶并视为对你使用一张【兵临城下】，然后你可使用剩余亮出牌并获得亮出的【闪】。",

  ["#plum__xiwei-prompt"] = "隙围：可以令一名其他角色将一至两张牌置于牌堆顶并视为对你使用一张【兵临城下】",
  ["#plum__xiwei-put"] = "隙围：将一至两张牌置于牌堆顶",
  ["#plum__xiwei-use"] = "隙围：可以使用以此法亮出的其余牌",
}

xiwei:addEffect("active", {
  anim_type = "control",
  prompt = "#plum__xiwei-prompt",
  can_use = function (self, player)
    return player:usedSkillTimes(xiwei.name, Player.HistoryPhase) == 0
  end,
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player and #to_select:getCardIds("he") > 0
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    if #target:getCardIds("he") > 0 then
      local cards = room:askToCards(target, {
        min_num = 1,
        max_num = 2,
        skill_name = xiwei.name,
        cancelable = false,
        include_equip = true,
        prompt = "#plum__xiwei-put",
      })
      if #cards > 0 then
        room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, xiwei.name, nil, false, target, nil, target.id)
      end
    end

    local card = Fk:cloneCard("enemy_at_the_gates")
    card.skillName = xiwei.name
    if not (target:isAlive() or player:isAlive()) then return end
    if target:canUseTo(card, player) then
      room:useCard{
        from = target,
        tos = { player },
        card = card,
      }
    end
  end,
})

xiwei:addEffect(fk.BeforeCardsMove, {
  mute = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(xiwei.name) then
      local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard)
      if not (use and use.data.card.skillName == xiwei.name) then return end

      local cards = {}
      for _, move in ipairs(data) do
        if move.toArea == Card.DiscardPile then
          for _, info in ipairs(move.moveInfo) do
            table.insertIfNeed(cards, info.cardId)
          end
        end
      end

      if #cards > 0 then
        event:setCostData(self, cards)
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cards = event:getCostData(self) ---@type integer[]
    while true do
      if not player:isAlive() then break end
      cards = table.filter(cards, function (id)
        return room:getCardArea(id) == Card.Processing
      end)
      if #cards == 0 then break end

      local use = room:askToUseRealCard(player, {
        pattern = cards,
        skill_name = xiwei.name,
        prompt = "#plum__xiwei-use",
        extra_data = { expand_pile = cards }
      })
      if not use then break end
    end

    if player:isAlive() then
      cards = table.filter(cards, function (id)
        return Fk:getCardById(id).trueName == "jink"
      end)
      if #cards > 0 then
        room:obtainCard(player, cards, true, fk.ReasonJustMove, player, xiwei.name, nil, table.map(room:getAlivePlayers(false), Util.IdMapper))
      end
    end

    for _, move in ipairs(data) do
      for i = #move.moveInfo, 1, -1 do
        if room:getCardArea(move.moveInfo[i].cardId) ~= Card.Processing then
          table.remove(move.moveInfo, i)
        end
      end
    end
  end
})

return xiwei